package com.gcsoftware.authentications.helper;

import com.gcsoftware.web.service.T_COM_SYS_USER_SERVICE;
import com.gcsoftware.authentications.beans.RoleInfo;
import com.gcsoftware.authentications.beans.UserInfo;
import com.gcsoftware.utils.SpringContextUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by qs on 2018/3/14.
 */

@Component
public class UserHelper {
    T_COM_SYS_USER_SERVICE userService = (T_COM_SYS_USER_SERVICE) SpringContextUtils.getBean(T_COM_SYS_USER_SERVICE.class);


    public UserInfo getUserByName(String userName) {
        UserInfo user = userService.getUserInfoByLoginName(userName);
        if (null != user) {
            List<RoleInfo> roleInfos =userService.queryUserRoles(user.getUserId());
            if (CollectionUtils.isNotEmpty(roleInfos)) {
                for (RoleInfo info : roleInfos) {
                    if (info.getRoleName().equals("ADMIN")) {
                        user.setUserRole("ADMIN");
                        break;
                    }
                }
                user.setUserRoles(roleInfos);
            }
            if (StringUtils.isBlank(user.getUserRole())) {
                user.setUserRole("USER");
            }

        }
        return user;
    }

   /* public static List<PermissionInfo> getPermissionsByUserId(String userId) {
        PermissionInfo permiss = new PermissionInfo();
        permiss.setPermisId("Pms_000000000001");
        permiss.setPermisName("/");

        List<PermissionInfo> list = new ArrayList<>();
        list.add(permiss);
        return list;

    }*/
}
